//
//  FR_Par_Attendance_ViewController.m
//  FaceRecognition
//
//  Created by zz on 17/7/19.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_Par_Attendance_ViewController.h"

#import "FR_Attendance_Model.h"
#import "SY_Par_Attendance_TableViewCell.h"
#import "ManagerInfoModel.h"
#import "TableHeaderView.h"

@interface FR_Par_Attendance_ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

/**选择学生*/
@property (nonatomic, strong) UITableView *selStuTableView;

@property (nonatomic, strong) NSMutableArray *classesDataArr;
/**列表数组*/
@property (nonatomic, strong) NSMutableArray *dataArr;

/**学生数组*/
@property (nonatomic, strong) NSMutableArray *stuArr;

@property (nonatomic, strong) UILabel *emptyLabel;

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, strong) TableHeaderView *tableHeaderView;

@property (nonatomic, assign) NSInteger selStuIndex;

@property (nonatomic, copy) NSString* selStuId; //所选择的学生id
@property (nonatomic, copy) NSString* selScid; //所选择的学生的scid



@property (nonatomic, strong) UIView *blackView;


@end

@implementation FR_Par_Attendance_ViewController {
    
    UIView *footerView;
    
    BOOL flag;
    
    UIButton *arrowBtn;
}

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, SCREENHEIGH - SafeAreaTopHeight - SafeAreaBottomHeight ) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self requestList];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self requestMoreList];
        }];
        
        _tableView.tableHeaderView = self.tableHeaderView;
        
        _tableView.rowHeight = 44;
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, VIEW_WIDTH, 15)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        [_tableView addSubview:_emptyLabel];
    }
    
    return _tableView;
}

//选择学生的tableViw
- (UITableView *)selStuTableView {
    
    if (!_selStuTableView) {
        
        _selStuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 44) style:UITableViewStylePlain];
        _selStuTableView.delegate = self;
        _selStuTableView.dataSource = self;
     
        _selStuTableView.separatorInset = UIEdgeInsetsZero;
        
        _selStuTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _selStuTableView.tableFooterView = nil;
        _selStuTableView.scrollEnabled = false;
        footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        footerView.backgroundColor = RGBACOLOR(0, 0, 0, 0);
    
        
        _selStuTableView.tableFooterView = footerView;

    }
    
    return _selStuTableView;
}

- (NSMutableArray *)classesDataArr {
    
    if (!_classesDataArr) {
        
        _classesDataArr = [NSMutableArray array];
    }
    
    return _classesDataArr;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

- (NSMutableArray *)stuArr {

    if (!_stuArr) {
        
        _stuArr = [NSMutableArray array];
    }
    
    return _stuArr;
}

- (UIView *)tableHeaderView {
    
    if (!_tableHeaderView) {
        
        _tableHeaderView = [[TableHeaderView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 112)];
        _tableHeaderView.backgroundColor = [UIColor whiteColor];
        
        [_tableHeaderView addSubview:self.selStuTableView];
        
        
        CGFloat rate = SCREENWIDTH / 375;
        
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * rate, 56, 345 / 2 * rate, 44)];
        nameLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        nameLabel.font = [UIFont systemFontOfSize:15];
        
        NSString *leftStr;
        NSString *rightStr;
        if (_arrival == -1) {  //正常考勤
            leftStr = @"到校时间";
            rightStr = @"状态";
        }else if (_arrival == 0){ //旷课
            leftStr = @"姓名";
            rightStr = @"旷课日期";
        }else if (_arrival == 2) {
            leftStr = @"姓名";
            rightStr = @"实际到校时间";
        }
        
        nameLabel.text  = leftStr;
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [_tableHeaderView addSubview:nameLabel];
        
        
        UILabel *arriveTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame), 56, 345 / 2 * rate, 44)];
        arriveTimeLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        arriveTimeLabel.font = [UIFont systemFontOfSize:15];
        arriveTimeLabel.textAlignment = NSTextAlignmentCenter;
        arriveTimeLabel.text = rightStr;
        [_tableHeaderView addSubview:arriveTimeLabel];
        
        
        UIView *grayViewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 44, VIEW_WIDTH, 12)];
        grayViewTop.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        [_tableHeaderView addSubview:grayViewTop];
        
        UIView *grayViewBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 100, VIEW_WIDTH, 12)];
        grayViewBottom.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        [_tableHeaderView addSubview:grayViewBottom];
    }
    
    return _tableHeaderView;
}

- (UIView *)blackView {
    
    if (!_blackView) {
        
        _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT-SafeAreaBottomHeight)];
        _blackView.backgroundColor = RGBACOLOR(0, 0, 0, 0.3);
        [self.view addSubview:_blackView];
        
        UITapGestureRecognizer *hideTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showStuAction)];
        [_blackView addGestureRecognizer:hideTap];
    }
    return _blackView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    flag = false;

    NSString *navTitle;
    
    if (_arrival == -1) {
        navTitle = @"考勤";
    }else if (_arrival == 0) {
        navTitle = @"旷课";
    }else if (_arrival == 2) {
        navTitle = @"迟到";
    }
    
    self.navBar.nav_titleLabel.text = navTitle;
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getAllChildrens];
    
    _selStuIndex = 0;
}

//获取家长身份下所有学生()
- (void)getAllChildrens{
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:USERID forKey:@"parentid"];
    
    [Utils Java_get:@"api/getChildClass" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        self.stuArr = [ManagerInfoModel mj_objectArrayWithKeyValuesArray:arr];
        
        if (self.stuArr.count > 0) {
            
            ManagerInfoModel *model = self.stuArr[0];
            _selStuId = model.studentid;
            _selScid = model.scid;
        }
        
        [self requestList];
        [self.selStuTableView reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    
}


- (void)requestList {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:@"1" forKey:@"pageno"];
    [dict setObject:@"20" forKey:@"pagesize"];
    NSString *scid = [SYAccountTool ShareAccount].account.scid;

    //ManagerInfoModel *stuModel = [self.stuArr objectAtIndex:_selStuIndex];
    
    [dict setObject:@"" forKey:@"date"];
    [dict setObject:@(_arrival) forKey:@"arrival"];
//    [dict setObject:stuModel.studentid forKey:@"studentid"];
    [dict setObject:_selStuId forKey:@"studentid"];
    if (_selScid.length > 0) {
        [dict setObject:_selScid forKey:@"scid"];
    }
    
    [Utils Java_get:@"api/getChildAttendanceList" parameters:dict success:^(NSInteger recode, id jsonObject) {
        _pageNo = 1;
        [self.dataArr removeAllObjects];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            
            _pageNo++;
            [self.tableView.mj_footer endRefreshing];
        }
        
        self.emptyLabel.hidden = arr.count != 0;
        
        self.dataArr = [FR_Attendance_Model mj_objectArrayWithKeyValuesArray:arr];
        
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
}

- (void)requestMoreList {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:@(_pageNo) forKey:@"pageno"];
    [dict setObject:@"20" forKey:@"pagesize"];
    
//    ManagerInfoModel *stuModel = [self.stuArr objectAtIndex:_selStuIndex];
    
    [dict setObject:@"" forKey:@"date"];
    [dict setObject:@(_arrival) forKey:@"arrival"];
//    [dict setObject:stuModel.studentid forKey:@"studentid"];
    [dict setObject:_selStuId forKey:@"studentid"];
    
    if (_selScid.length > 0) {
        [dict setObject:_selScid forKey:@"scid"];
    }

    
    [Utils Java_get:@"api/getChildAttendanceList" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            
            _pageNo++;
            [self.tableView.mj_footer endRefreshing];
        }
        
        NSArray *arrM = [FR_Attendance_Model mj_objectArrayWithKeyValuesArray:arr];
        [self.dataArr addObjectsFromArray:arrM];
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self.tableView.mj_footer endRefreshing];
        [self showHint:error.localizedDescription];
    }];
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == _tableView) {
        
        return self.dataArr.count;
    }
    
    return  self.stuArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (tableView == _tableView) {
        
        static NSString *cellid = @"cellid";
        
        SY_Par_Attendance_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        
        if (!cell) {
            
            cell = [[SY_Par_Attendance_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        
        cell.arrival = _arrival;
        cell.model = [self.dataArr objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    static NSString *stuCellid = @"stucellid";
    
    //不会复用的
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:stuCellid];
    
    cell.textLabel.frame = CGRectMake(15, 0, 300, 44);
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(15, 43.5, SCREENWIDTH - 15, 0.5)];
    lineV.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
    
    [cell.contentView addSubview:lineV];
    
    
    if (indexPath.row == 0) {
        
        arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [arrowBtn setImage:[UIImage imageNamed:@"record_head_classchoose_down_"] forState:UIControlStateNormal];
        [arrowBtn setImage:[UIImage imageNamed:@"record_head_classchoose_up_"] forState:UIControlStateSelected];
        arrowBtn.frame = CGRectMake(SCREENWIDTH - 15 - 50, -5, 50, 50);
        [arrowBtn addTarget:self action:@selector(showStuAction) forControlEvents:UIControlEventTouchUpInside];
        
        
        arrowBtn.hidden = self.stuArr.count == 1;
        
        lineV.x  = 0;
        lineV.width = SCREENWIDTH;
        
        lineV.hidden = !flag;
        
        [cell.contentView addSubview:arrowBtn];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ManagerInfoModel *model = [self.stuArr objectAtIndex:indexPath.row];
    
    if (indexPath.row == self.dataArr.count - 1) {
        
        lineV.hidden = YES;
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",model.scname,model.studentname];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _selStuTableView) {
        
        if (flag == NO) { //非展开状态 不可点击
            return;
        }
        
        if (flag == true) {
            
            [self showStuAction]; //首先弹回
        }
        
        _selStuIndex = indexPath.row;
        
        ManagerInfoModel *selModel = [self.stuArr objectAtIndex:indexPath.row];
        _selStuId = selModel.studentid;
        _selScid = selModel.scid;
        [self requestList];
        
        ManagerInfoModel *model = [self.stuArr objectAtIndex:_selStuIndex];
        
        ManagerInfoModel *model0 = [self.stuArr objectAtIndex:0];
        
        [self.stuArr replaceObjectAtIndex:0 withObject:model];
        
        [self.stuArr replaceObjectAtIndex:_selStuIndex withObject:model0];
        
        [self.selStuTableView reloadData];
        
        
    }
}

#pragma mark -Action
- (void)showStuAction {
    
    [_tableHeaderView bringSubviewToFront:self.selStuTableView];
    
    self.selStuTableView.backgroundColor = RGBACOLOR(0, 0, 0, 0.3);
    
    self.selStuTableView.height = flag == false ? _stuArr.count * 44 : 44;
    self.tableHeaderView.height = flag == false ? _selStuTableView.height : 112;
    self.emptyLabel.hidden = !flag;
    self.blackView.y = CGRectGetMaxY(self.selStuTableView.frame) + SafeAreaTopHeight;
    self.blackView.height = SCREENHEIGH - SafeAreaTopHeight-SafeAreaBottomHeight-self.selStuTableView.height;
    self.blackView.hidden = flag == false ? NO  : YES;
    
    self.tableView.scrollEnabled = flag ? YES : NO; //展开状态下禁止scrollview滑动 关闭状态允许滑动
    
    arrowBtn.selected = !flag;
    
    flag = !flag;
    
    [self.selStuTableView reloadData];
}
@end

